package com.boil.qz.safekavass.weixin.util;

import com.alibaba.fastjson.JSONObject;
import com.boil.qz.safekavass.weixin.model.menu.ComplexButton;
import com.boil.qz.safekavass.weixin.model.menu.Menu;
import com.boil.qz.safekavass.weixin.model.menu.ViewButton;
import com.boil.util.Constants;
import com.boil.util.FastJsonUtils;
import org.junit.Test;

/**
 * Created by ChenYong on 2017-05-22.
 * <br>
 * 微信菜单工具类。
 */
public class WeixinMenuUtils {
    /**
     * 创建菜单。
     */
    @Test
    public void create() {
        // 我的举报按钮
        ViewButton myReportViewButton = new ViewButton();
        myReportViewButton.setName("我的举报");
        myReportViewButton.setType(WeixinUtils.VIEW_STR);
        myReportViewButton.setUrl(String.format(WeixinUtils.URL_CONNECT_OAUTH2_AUTHORIZE, //
                WeixinUtils.APP_ID, //
                Constants.URL_ENCODE_WEIXIN_WEBPAGE_AUTHORIZE_SNSAPI_BASE, //
                WeixinUtils.SCOPE_SNSAPI_BASE, //
                Constants.PAGE_MY_REPORT));

        // 我要举报按钮
        ViewButton reportViewButton = new ViewButton();
        reportViewButton.setName("我要举报");
        reportViewButton.setType(WeixinUtils.VIEW_STR);
        reportViewButton.setUrl(String.format(WeixinUtils.URL_CONNECT_OAUTH2_AUTHORIZE, //
                WeixinUtils.APP_ID, //
                Constants.URL_ENCODE_WEIXIN_WEBPAGE_AUTHORIZE_SNSAPI_USERINFO, //
                WeixinUtils.SCOPE_SNSAPI_USERINFO, //
                Constants.PAGE_REPORT));

        // 举报按钮
        ComplexButton reportComplexButton = new ComplexButton();
        reportComplexButton.setName("举报");
        reportComplexButton.getSub_button().add(myReportViewButton);
        reportComplexButton.getSub_button().add(reportViewButton);

        // 绑定手机号按钮
        ViewButton bindPhoneViewButton = new ViewButton();
        bindPhoneViewButton.setName("绑定手机号");
        bindPhoneViewButton.setType(WeixinUtils.VIEW_STR);
        bindPhoneViewButton.setUrl(String.format(WeixinUtils.URL_CONNECT_OAUTH2_AUTHORIZE, //
                WeixinUtils.APP_ID, //
                Constants.URL_ENCODE_WEIXIN_WEBPAGE_AUTHORIZE_SNSAPI_USERINFO, //
                WeixinUtils.SCOPE_SNSAPI_USERINFO, //
                Constants.PAGE_BIND_PHONE));

        // 使用说明按钮
        ViewButton reportUseViewButton = new ViewButton();
        reportUseViewButton.setName("使用说明");
        reportUseViewButton.setType(WeixinUtils.VIEW_STR);
        reportUseViewButton.setUrl("http://mp.weixin.qq.com/s/zPK5PhNLy_sZ2tPCkannLQ");

        // 菜单
        Menu menu = new Menu();
        menu.getButton().add(reportComplexButton);
        menu.getButton().add(bindPhoneViewButton);
        menu.getButton().add(reportUseViewButton);

        // access_token
        String accessToken = "kWP9iWvfatvkFh9CUFzKQ9neY5-Gfek1zDT2LEKyRs2EXkGZpIWLM9iCRsZ8cz1ldL_7g83m52MSOf0nOK2sYUm5Ce8ux9-gBNcCn0a-s4gUFVbAEANYO";
        // 创建菜单
        String menuJson = FastJsonUtils.bean2Json(menu);
        JSONObject result = WeixinUtils.sendHttpsPost(String.format(WeixinUtils.URL_POST_CREATE_MENU, accessToken), menuJson);
        System.out.println("1---------->" + menuJson);
        System.out.println("2---------->" + result.toJSONString());
    }
}